# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=blender

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
blender_version=2.70
pkgver=r54469.a6a38de
pkgrel=1
pkgdesc="A fully integrated 3D graphics creation suite (mingw-w64)"
arch=('any')
license=('GPL')
url="http://www.blender.org"
depends=("${MINGW_PACKAGE_PREFIX}-boost"
        "${MINGW_PACKAGE_PREFIX}-glew"
        "${MINGW_PACKAGE_PREFIX}-ffmpeg"
        "${MINGW_PACKAGE_PREFIX}-fftw"
        "${MINGW_PACKAGE_PREFIX}-freetype"
        "${MINGW_PACKAGE_PREFIX}-libpng"
        "${MINGW_PACKAGE_PREFIX}-libsndfile"
        "${MINGW_PACKAGE_PREFIX}-libtiff"
        "${MINGW_PACKAGE_PREFIX}-openexr"
        "${MINGW_PACKAGE_PREFIX}-openal"
        "${MINGW_PACKAGE_PREFIX}-opencollada"
        "${MINGW_PACKAGE_PREFIX}-opencolorio"
        "${MINGW_PACKAGE_PREFIX}-openimageio"
        "${MINGW_PACKAGE_PREFIX}-openshadinglanguage"
        "${MINGW_PACKAGE_PREFIX}-python3"
        "${MINGW_PACKAGE_PREFIX}-python3-numpy"
        "${MINGW_PACKAGE_PREFIX}-wintab-sdk")
makedepends=("${MINGW_PACKAGE_PREFIX}-boost" "${MINGW_PACKAGE_PREFIX}-clang" "${MINGW_PACKAGE_PREFIX}-cmake")
#[[ $CARCH == x86_64 ]] && makedepends+=('cuda')
optdepends=('cuda: cycles renderer cuda support')
options=(!strip staticlibs !buildflags)
#install=blender.install
source=("${_realname}"::"git://git.blender.org/blender.git"
        "translations"::"git://git.blender.org/blender-translations.git"
        "addons"::"git://git.blender.org/blender-addons.git"
        "addons-contrib"::"git://git.blender.org/blender-addons-contrib.git"
        "mingw-w64-cmake.patch"
        "fix-mingw-w64-definitions.patch"
        "mingw-dont-install-3rdparty.patch"
        "fix-osl.patch")
md5sums=('SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'f3bca1b341a6af6b1a78e1407bbb29f8'
         'f3a3b8e527534bc15e2e90f7c3aec235'
         '1969914e11d0a4f74dbfeddad742c9da'
         'cd59e2ed1d0d4318e6c200436b3277e5')

pkgver() {
  cd "$srcdir/$_realname"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "$srcdir/$_realname"
  patch -p1 -i ${srcdir}/mingw-w64-cmake.patch
  patch -p1 -i ${srcdir}/fix-mingw-w64-definitions.patch
  patch -p1 -i ${srcdir}/mingw-dont-install-3rdparty.patch
  patch -p1 -i ${srcdir}/fix-osl.patch
  
  cp -rf $srcdir/translations/* $srcdir/$_realname/release/datafiles/locale/
  cp -rf $srcdir/addons/* $srcdir/$_realname/release/scripts/addons/
  cp -rf $srcdir/addons-contrib/* $srcdir/$_realname/release/scripts/addons_contrib/
}

build() {
  
  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null

  mkdir $srcdir/build-${MINGW_CHOST}
  cd $srcdir/build-${MINGW_CHOST}

  [[ $CARCH == i686 ]] && ENABLESSE2="-DSUPPORT_SSE2_BUILD=OFF"
  [[ $CARCH == i686 ]] && BUILDCUDA="OFF" || BUILDCUDA="ON"
  [[ $CARCH == i686 ]] && PMARCH="i686" || PMARCH="x86-64"
  #unset CFLAGS
  #unset CXXFLAGS
  #CFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"
  #CXXFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"
  CFLAGS="-O2 -pipe -march=${PMARCH} -mtune=generic -D__USE_MINGW_ANSI_STDIO=1"
  CXXFLAGS="$CFLAGS"
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DMINGW_LIBS=${PREFIX_DEPS} \
    -DCMAKE_BUILD_TYPE=Release \
    -DWITH_SYSTEM_GLEW=ON \
    -DWITH_SYSTEM_OPENJPEG=ON \
    -DWITH_INSTALL_PORTABLE=OFF \
    -DWITH_OPENCOLLADA=ON \
    -DWITH_GAMEENGINE=ON \
    -DWITH_RAYOPTIMIZATION=ON \
    -DWITH_JACK=OFF \
    -DWITH_PLAYER=ON \
    -DWITH_CODEC_FFMPEG=ON \
    -DWITH_CODEC_SNDFILE=ON \
    -DWITH_CYCLES=ON \
    -DWITH_CYCLES_CUDA_BINARIES=OFF \
    -DWITH_CYCLES_OSL=ON \
    -DWITH_FFTW3=ON \
    -DWITH_MOD_OCEANSIM=ON \
    -DPYTHON_VERSION=3.4 \
    -DPYTHON_LIBPATH=${PREFIX_DEPS}/lib \
    -DPYTHON_LIBRARY=python3.4m \
    -DPYTHON_INCLUDE_DIRS=${PREFIX_DEPS}/include/python3.4m \
    -DWITH_PYTHON_INSTALL=OFF \
    -DWITH_PYTHON_INSTALL_NUMPY=OFF \
    -DPYTHON_NUMPY_PATH=${PREFIX_DEPS}/lib/python3.4/site-packages \
    -DCYCLES_OSL=${PREFIX_DEPS} \
    -DWITH_IMAGE_REDCODE=ON \
    -DWITH_OPENMP=ON \
    ../${_realname}

  # Remove the openjpeg line once blender accepts my patch
  make # -j1 VERBOSE=1
}

package() {  
  cd "$srcdir/build-${MINGW_CHOST}"
  make install
  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib
  cp -r bin ${pkgdir}${MINGW_PREFIX}/
  rm ${pkgdir}${MINGW_PREFIX}/bin/msgfmt.exe
  mv ${pkgdir}${MINGW_PREFIX}/bin/*.a ${pkgdir}${MINGW_PREFIX}/lib/

  ${MINGW_PREFIX}/bin/python3 -m compileall "${pkgdir}${MINGW_PREFIX}/bin/$blender_version"
  ${MINGW_PREFIX}/bin/python3 -m compileall \
  ${pkgdir}${MINGW_PREFIX}/bin/$blender_version/scripts/startup \
  ${pkgdir}${MINGW_PREFIX}/bin/$blender_version/scripts/modules \
  ${pkgdir}${MINGW_PREFIX}/bin/$blender_version/scripts/addons
}
